<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * @package	
 */
class HelpDeskHelperEmail 
{
	/**
	 * 
	 * @return unknown_type
	 */
	function getMailer()
	{
		global $mosConfig_absolute_path, $mosConfig_sendmail;
		global $mosConfig_smtpauth, $mosConfig_smtpuser;
		global $mosConfig_smtppass, $mosConfig_smtphost;
		global $mosConfig_mailfrom, $mosConfig_fromname, $mosConfig_mailer;
		
		$sendmail 	= $mosConfig_sendmail;
		$smtpauth 	= $mosConfig_smtpauth;
		$smtpuser 	= $mosConfig_smtpuser;
		$smtppass  	= $mosConfig_smtppass;
		$smtphost 	= $mosConfig_smtphost;
		$mailfrom 	= $mosConfig_mailfrom;
		$fromname 	= $mosConfig_fromname;
		$mailer 	= $mosConfig_mailer;

		// Create a HelpDeskMail object
		$mail 		=& HelpDeskMail::getInstance();

		// Set default sender
		$mail->setSender(array ($mailfrom, $fromname));

		// Default mailer is to use PHP's mail function
		switch ($mailer)
		{
			case 'smtp' :
				$mail->useSMTP($smtpauth, $smtphost, $smtpuser, $smtppass);
				break;
			case 'sendmail' :
				$mail->useSendmail($sendmail);
				break;
			default :
				$mail->IsMail();
				break;
		}

		return $mail;
	}

	/**
	 * 
	 * @return unknown_type
	 */
	function _doMail( $from, $fromname, $recipient, $subject, $body, $actions=NULL, $mode=NULL, $cc=NULL, $bcc=NULL, $attachment=NULL, $replyto=NULL, $replytoname=NULL ) 
	{
		$success = false;

		$message = HelpDeskHelperEmail::getMailer();
		$message->addRecipient( $recipient );
		$message->setSubject( $subject );
		$message->setBody( $body );
		$sender = array( $from, $fromname );
		$message->setSender($sender);
		$sent = $message->send();
		if ($sent == '1') {
			$success = true;
		}
		
		return $success;
	}
	
	/**
	 * Sends a formatted email
	 * @param mixed 
	 * @return boolean
	 */					
	function &sendMail( $from, $fromname, $recipient, $subject, $body, $actions=NULL, $mode=NULL, $cc=NULL, $bcc=NULL, $attachment=NULL, $replyto=NULL, $replytoname=NULL ) 
	{
		$body .= HelpDeskText::_( 'Email Signature' );			
		$send = HelpDeskHelperEmail::_doMail( $from, $fromname, $recipient, $subject, $body, $mode, $cc, $bcc, $attachment, $replyto, $replytoname );
		return $send;
	}

	/**
	 * Returns an array of user objects
	 * @param mixed Boolean
	 * @param mixed Boolean
	 * @return array
	 */
	function &getEmailRecipients( $categoryid, $ticket='' ) {
		// all managers of category and parent
		$managers = &HelpDeskHelperHelpDesk::getCategoryManagers( $categoryid );
		$recipients = $managers;
		
		// check config -- does system admin receive email?
		// if so array_merge

		// check config -- does person performing action receive email?
		// if so array_merge
		
		if (isset($ticket->sender_userid)) {
			global $database;
			$sender = new mosUser( $database );
			$sender->load((int) $ticket->sender_userid );
			$recipients[] = $sender;
		}
		
		return $recipients;		
	}

	/**
	 * Returns 
	 * @param object A valid ticket object
	 * @param mixed Boolean
	 * @param mixed Boolean
	 * @return array
	 */
	function &getEmailContent( $ticket, $createdby, $messagetype='1' ) {
		
		$return = array();
		$link = JURI_BASE."/index.php?option="._THISCOMPONENT."&section=ticket&task=view&id=".$ticket->id;
		// $link = JRoute::_( $link, false );

		// get config settings
		$config = &HelpDeskConfig::getInstance();
		$emails_includedescription 		= $config->get( 'emails_includedescription', '0' );
		$emails_descriptionmaxlength 	= $config->get( 'emails_descriptionmaxlength', '-1' );
		$emails_includecomments 		= $config->get( 'emails_includecomments', '0' );
		$emails_commentmaxlength		= $config->get( 'emails_commentmaxlength', '-1' );		
		
		switch ($messagetype) {
			case "3": // add attachment
				$return['subject'] = HelpDeskText::_( 'New Attachment Email Subject' );
				$return['body'] = sprintf( HelpDeskText::_( 'New Attachment Email Message' ), $createdby, $link);
			  break;
			case "2": // add comment
				$return['subject'] = HelpDeskText::_( 'New Comment Email Subject' );
				if ($emails_includecomments) {
					// if include, trim and set body
					if ($emails_commentmaxlength > 0) {
						$text = substr( nl2br( stripslashes( $ticket->comment ) ), 0, $emails_commentmaxlength );
					} else {
						$text = nl2br( stripslashes( $ticket->comment ) );
					}
					$return['body'] = $text;
				} else {
					// else 
					$return['body'] = sprintf( HelpDeskText::_( 'New Comment Email Message' ), $createdby, $link);
				}
			  break;
			case "1": // new ticket
			default:
				$return['subject'] = HelpDeskText::_( 'New Ticket Email Subject' );
				if ($emails_includedescription) {
					// if include, trim and set body
					if ($emails_descriptionmaxlength > 0) {
						$text = substr( nl2br( stripslashes( $ticket->description ) ), 0, $emails_descriptionmaxlength );
					} else {
						$text = nl2br( stripslashes( $ticket->description ) );
					}
					$return['body'] = $text;
				} else {
					// else 
					$return['body'] = sprintf( HelpDeskText::_( 'New Ticket Email Message' ), $createdby, $link);
				}
			  break;
		}
		
		return $return;

	}

	/**
	 * Returns 
	 * @param mixed Boolean
	 * @param array Email content
	 * @return boolean
	 */
	function &sendEmailNotices( $ticket, $createdby, $messagetype='1' ) {
		global $mainframe;
		$success = false;
		$done = array();
		
		// grab config settings for sender name and email
		$config = &HelpDeskConfig::getInstance();
		$mailfrom 	= $config->get( 'emails_defaultemail', $mainframe->getCfg('mailfrom') );
		$fromname 	= $config->get( 'emails_defaultname', $mainframe->getCfg('fromname') );
		
		$recipients = &HelpDeskHelperEmail::getEmailRecipients( $ticket->categoryid, $ticket );
		$content = &HelpDeskHelperEmail::getEmailContent( $ticket, $createdby, $messagetype );
		
		for ($i=0; $i<count($recipients); $i++) {
			$recipient = $recipients[$i];
			if (!isset($done[$recipient->email])) {
				if ( $send = &HelpDeskHelperEmail::sendMail( $mailfrom, $fromname, $recipient->email, $content['subject'], $content['body'] ) ) {
					$success = true;
					$done[$recipient->email] = $recipient->email;
				}	
			}
		}

		return $success;
	}
}